#include <stdio.h>
#include <stdlib.h>

typedef struct LNode
{
    int data;
    struct LNode *prv;
    struct LNode *next;
}LNode,*DoublyLinkeList;
void InitLinkeList(DoublyLinkeList *L,int n);
void InitLinkeList(DoublyLinkeList *L,int n)
{
    *L=(DoublyLinkeList)malloc(sizeof(LNode));
    (*L)->next=NULL;
    (*L)->prv=NULL;
    DoublyLinkeList temp=*L;
    if(*L==NULL)
    {
        printf("ERROR!");
        return;
    }
    for(int i=0;i<n;i++)
    {
        DoublyLinkeList p=(DoublyLinkeList)malloc(sizeof(LNode));
        if(p==NULL)
        {
            printf("ERROR!");
            return;
        }
        printf("请输入第%d个节点的值",i+1);
        scanf("%d",&p->data);
        temp->next=p;
        p->prv=temp;
        temp=temp->next;
    }
}
void GetElem(DoublyLinkeList L,int sort,int *elem);
void GetElem(DoublyLinkeList L,int sort,int *elem)
{
    DoublyLinkeList p=L;
    int i=0;
    while(p && i<sort)
    {
        p=p->next;
        i++;
    }
    if(p==NULL)
    {
        printf("ERROR!");
        return;
    }
    *elem=p->data;
}
void Insert(DoublyLinkeList L,int sort,int elem);
void Insert(DoublyLinkeList L,int sort,int elem)
{
    int i=0;
    DoublyLinkeList p=L;
    DoublyLinkeList new=(DoublyLinkeList)malloc(sizeof(LNode));
    if(new==NULL)
    {
        printf("ERROR!");
        return;
    }
    while(p && i<sort)
    {
        p=p->next;
        i++;
    }
    if(p==NULL)
    {
        printf("ERROR!");
        return;
    }
    new->next=p;
    p->prv->next=new;
    new->prv=p->prv;
    p->prv=new;
    new->data=elem;
}
void Delete(DoublyLinkeList L,int sort);
void Delete(DoublyLinkeList L,int sort)
{
    int i=0;
    DoublyLinkeList p=L;
    while(p && i<sort)
    {
        p=p->next;
        i++;
    }
    if(p==NULL)
    {
        printf("ERROR!");
        return;
    }
    p->next->prv=p->prv;
    p->prv->next=p->next;
    free(p);
}
void Delete_Content(DoublyLinkeList L,int elem);
void Delete_Content(DoublyLinkeList L,int elem)
{
    DoublyLinkeList p=L;
    while(p && p->data!=elem)
    {
        p=p->next;
    }
    if(p==NULL)
    {
        printf("ERROR!");
        return;
    }
    p->next->prv=p->prv;
    p->prv->next=p->next;
    free(p);
}
int main()
{
    DoublyLinkeList L;
    int n,elem;
    printf("请输入双向链表的长度：");
    scanf("%d",&n);
    InitLinkeList(&L,n);
    GetElem(L,2,&elem);
    printf("%d\n",elem);
    Insert(L,2,8);
    GetElem(L,2,&elem);
    printf("%d\n",elem);
    Delete(L,2);
    GetElem(L,2,&elem);
    printf("%d\n",elem);
    Delete_Content(L,2);
    GetElem(L,2,&elem);
    printf("%d\n",elem);
}